package com.parkingspace.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.parkingspace.dao.PropertyBetweenUserMapper;
import com.parkingspace.dao.PropertyMapper;
import com.parkingspace.dao.UserMapper;
import com.parkingspace.entity.Property;
import com.parkingspace.entity.PropertyBetweenUser;
import com.parkingspace.entity.User;
import com.parkingspace.service.IUserService;
import com.parkingspace.util.JwtUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Queue;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author Gapper
 * @since 2021-05-24
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

	@Resource
	private UserMapper userMapper;

	@Resource
	private PropertyBetweenUserMapper propertyBetweenUserMapper;

	@Resource
	private PropertyMapper propertyMapper;

	@Override
	public Property findByProperty(String token) throws Exception {
		String username = JwtUtils.getUsername(token);
		QueryWrapper<User> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("uname", username);
		User user = userMapper.selectOne(queryWrapper);
		if (user == null) {
			throw new RuntimeException("请先进行登录!");
		}

		//查询物业公司ID
		QueryWrapper<PropertyBetweenUser> q = new QueryWrapper<>();
		q.eq("pbu_user_id", user.getUid());
		PropertyBetweenUser propertyBetweenUser = propertyBetweenUserMapper.selectOne(q);
		if (propertyBetweenUser == null) {
			throw new RuntimeException("参数传入错误!");
		}

		//查询物业公司详细信息
		QueryWrapper<Property> q2 = new QueryWrapper<>();
		q2.eq("property_id", propertyBetweenUser.getPbuPropertyId());
		Property property = propertyMapper.selectOne(q2);
		if (property == null) {
			throw new RuntimeException("查询失败!");
		}
		return property;
	}
}
